package com.itheima.config;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DriverManagerDataSource;

import javax.sql.DataSource;

/**
 * ClassName:JdbcConfig
 * Package:com.itheima.config
 * Description:和连接数据库相关的配置类
 * <p>
 * <!--    配置jdbcTemplate-->
 * <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
 * <property name="dataSource" ref="dataSource"></property>
 * </bean>
 * <!-- 配置数据源 -->
 * <bean id="dataSource"
 * class="org.springframework.jdbc.datasource.DriverManagerDataSource">
 * <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
 * <property name="url" value="jdbc:mysql://localhost:3306/ssm"/>
 * <property name="username" value="root"/>
 * <property name="password" value="123"/>
 * </bean>
 * Date:2020/3/4 1:23
 * User: snow
 */
public class JdbcConfig {
	@Value("${jdbc.driver}")
	private String driver;
	@Value("${jdbc.url}")
	private String url;
	@Value("${jdbc.username}")
	private String username;
	@Value("${jdbc.password}")
	private String password;

	/**
	 * 创建JdbcTemplate
	 *
	 * @param dataSource
	 * @return
	 */
	@Bean(name = "jdbcTemplate")
	private JdbcTemplate createJdbcTemplate(DataSource dataSource) {
		return new JdbcTemplate(dataSource);
	}

	/*	*//**
	 * 创建数据源对象
	 * @return
	 *//*
	@Bean(name = "dataSource")
	public DataSource dataSource(){
		DriverManagerDataSource dataSource = new DriverManagerDataSource();
		dataSource.setDriverClassName("com.mysql.jdbc.Driver");
		dataSource.setUrl("jdbc:mysql://localhost:3306/ssm");
		dataSource.setUsername("root");
		dataSource.setPassword("123");
		return dataSource;
	}
	*/

	/**
	 * 创建数据源对象
	 *
	 * @return
	 */
	@Bean(name = "dataSource")
	public DataSource dataSource() {
		DriverManagerDataSource dataSource = new DriverManagerDataSource();
		dataSource.setDriverClassName(driver);
		dataSource.setUrl(url);
		dataSource.setUsername(username);
		dataSource.setPassword(password);
		return dataSource;
	}


}
